// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package exploitdb

import (
	"reflect"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	exploitmodels "github.com/vulsio/go-exploitdb/models"

	"github.com/openclarity/openclarity/scanner/families/exploits/types"
)

func TestConvertToCommonExploits(t *testing.T) {
	type args struct {
		es    []exploitmodels.Exploit
		cveID string
	}
	tests := []struct {
		name         string
		args         args
		wantExploits []types.Exploit
	}{
		{
			name: "sanity",
			args: args{
				es: []exploitmodels.Exploit{
					{
						ID:              1,
						ExploitType:     "exploitType1",
						ExploitUniqueID: "exploitUniqueID1",
						URL:             "http://bla:80",
						Description:     "desc 1",
						CveID:           "cve1",
					},
					{
						ID:              2,
						ExploitType:     "exploitType2",
						ExploitUniqueID: "exploitUniqueID2",
						URL:             "http://bla:8080",
						Description:     "desc 2",
						CveID:           "cve1",
					},
				},
				cveID: "cve1",
			},
			wantExploits: []types.Exploit{
				{
					ID:          "exploitUniqueID1",
					Name:        "",
					Title:       "",
					Description: "desc 1",
					CveID:       "cve1",
					URLs:        []string{"http://bla:80"},
					SourceDB:    "exploitType1",
				},
				{
					ID:          "exploitUniqueID2",
					Name:        "",
					Title:       "",
					Description: "desc 2",
					CveID:       "cve1",
					URLs:        []string{"http://bla:8080"},
					SourceDB:    "exploitType2",
				},
			},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotExploits := convertToCommonExploits(tt.args.es, tt.args.cveID)
			if diff := cmp.Diff(tt.wantExploits, gotExploits, cmpopts.SortSlices(func(a, b types.Exploit) bool { return a.ID < b.ID })); diff != "" {
				t.Errorf("convertToCommonExploits() mismatch (-want +got):\n%s", diff)
			}
		})
	}
}

func Test_stringToArray(t *testing.T) {
	type args struct {
		str string
	}
	tests := []struct {
		name string
		args args
		want []string
	}{
		{
			name: "empty string",
			args: args{
				str: "",
			},
			want: nil,
		},
		{
			name: "trim spaces",
			args: args{
				str: "cve1, cve2,  cve3",
			},
			want: []string{"cve1", "cve2", "cve3"},
		},
		{
			name: "sanity",
			args: args{
				str: "cve1,cve2,cve3",
			},
			want: []string{"cve1", "cve2", "cve3"},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := stringToArray(tt.args.str); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("stringToArray() = %v, want %v", got, tt.want)
			}
		})
	}
}
